import Vue from "vue";
import VueRouter from "vue-router";
import LoginViews from "../views/login/LoginViews.vue";
import RegisterViews from "../views/regiseter/RegisterViews.vue";
import HomeViews from "../views/home/HomeViews.vue";
import StudentList from "../views/studentList/StudentList.vue";
import StudentAdd from "../views/studentAdd/StudentAdd.vue";
import StudentUpdate from "../views/studentList/StudentUpdate.vue";
import ClassList from "../views/classList/ClassList.vue";
import ClassAdd from "../views/classAdd/ClassAdd.vue";
import ClassUpdate from "../views/classList/ClassUpdate.vue";
import SubjectsList from "../views/subjectsList/SubjectsList.vue";
import CoursesList from "../views/coursesList/CoursesList.vue";
import CoursesAdd from "../views/coursesAdd/CoursesAdd.vue";
import TeacherList from "../views/teacherList/TeacherList.vue";
import TeacherAdd from "../views/teacherAdd/TeacherAdd.vue";
import TeacherUpdate from "../views/teacherList/TeacherUpdate.vue";
import api from "@/api";
import store from "@/store";

const NotFound = () => import("../views/NotFound/NotFound.vue");
//路由懒加载
// function NotFound() {
//   return import("../views/NotFound/NotFound.vue");
// }
Vue.use(VueRouter);

const routes = [
  //路由重定向（一般放在所有一般路由的上方）
  {
    path: "/",
    redirect: "/login",
  },
  //一般路由
  {
    path: "/home", //浏览器路径
    name: "Home", //路由名称（可写可不写）
    component: HomeViews, //组件
    beforeEnter: async (to, from, next) => {
      if (to.path.includes) {
        const token = localStorage.token;
        if (token) {
          //根据token获取用户信息
          const res = await api.login.getUserInfo();
          console.log(res);
          if (res.code) {
            //调用 mutations 将用户信息保存到state
            store.commit("SET_USER_INFO", res.data);
          }
          next();
        } else {
          alert("您还未登录，请登录");
          next("/login");
        }
      } else {
        next();
      }
    },
    children: [
      {
        path: "studentList", //子路由PATH不要以 / 开头
        name: "StudentList", //路由名称（可写可不写）
        component: StudentList, //组件
        meta: {
          title: ["学生管理", "学生列表"],
        },
      },
      {
        path: "studentAdd", //子路由PATH不要以 / 开头
        name: "StudentAdd", //路由名称（可写可不写）
        component: StudentAdd, //组件
        meta: {
          title: ["学生管理", "新增学生"],
          isKeepAlive: true,
        },
      },
      {
        path: "studentUpdate/:id", //   /:id是动态路由(id自己可以写成任意名字) 如果在id后面加？那么是与不是动态路由都可访问
        name: "StudentUpdate", //路由名称（可写可不写）
        component: StudentUpdate, //组件
        props: true, //获取动态路由参数需要添加props属性
        meta: {
          title: ["学生管理", "修改学生"],
        },
      },
      {
        path: "classList", //子路由PATH不要以 / 开头
        name: "ClassList", //路由名称（可写可不写）
        component: ClassList, //组件
        meta: {
          title: ["学生管理", "班级列表"],
        },
      },
      {
        path: "classAdd", //子路由PATH不要以 / 开头
        name: "ClassAdd", //路由名称（可写可不写）
        component: ClassAdd, //组件
        meta: {
          title: ["学生管理", "新增班级"],
          isKeepAlive: true,
        },
      },
      {
        path: "classUpdate/:id", //   /:id是动态路由(id自己可以写成任意名字) 如果在id后面加？那么是与不是动态路由都可访问
        name: "ClassUpdate", //路由名称（可写可不写）
        component: ClassUpdate, //组件
        props: true, //获取动态路由参数需要添加props属性
        meta: {
          title: ["学生管理", "修改班级"],
        },
      },
      {
        path: "subjectsList", //子路由PATH不要以 / 开头
        name: "SubjectsList", //路由名称（可写可不写）
        component: SubjectsList, //组件
        meta: {
          title: ["学生管理", "专业列表"],
        },
      },

      {
        path: "coursesList", //子路由PATH不要以 / 开头
        name: "CoursesList", //路由名称（可写可不写）
        component: CoursesList, //组件
        meta: {
          title: ["学生管理", "课程列表"],
        },
      },
      {
        path: "coursesAdd", //子路由PATH不要以 / 开头
        name: "CoursesAdd", //路由名称（可写可不写）
        component: CoursesAdd, //组件
        meta: {
          title: ["学生管理", "新增课程"],
          isKeepAlive: true,
        },
      },
      {
        path: "teacherList", //子路由PATH不要以 / 开头
        name: "TeacherList", //路由名称（可写可不写）
        component: TeacherList, //组件
        meta: {
          title: ["学生管理", "修教师列表"],
        },
      },
      {
        path: "teacherAdd", //子路由PATH不要以 / 开头
        name: "TeacherAdd", //路由名称（可写可不写）
        component: TeacherAdd, //组件
        meta: {
          title: ["学生管理", "新增教师"],
          isKeepAlive: true,
        },
      },
      {
        path: "teacherUpdate/:id", //   /:id是动态路由(id自己可以写成任意名字) 如果在id后面加？那么是与不是动态路由都可访问
        name: "TeacherUpdate", //路由名称（可写可不写）
        component: TeacherUpdate, //组件
        props: true, //获取动态路由参数需要添加props属性
        meta: {
          title: ["学生管理", "修改教师"],
        },
      },
    ],
  },
  {
    path: "/login",
    name: "Login",
    component: LoginViews,
  },
  {
    path: "/register",
    name: "Register",
    component: RegisterViews,
  },
  //404页面
  {
    path: "*",
    name: "NotFound",
    component: NotFound,
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

//全局守卫，前置守卫
// router.beforeEach((to, from, next) => {
//   if (to.path.includes('home')) {
//     const token = localStorage.token;
//     if (token) {
//       api.login.getUserInfo().then((res)=>{
//         console.log(res);
//       })
//     } else {
//       alert("您还未登录，请登录");
//       next("/login");
//     }
//   } else {
//     next();
//   }
// });

export default router;
